package com.wwh.memo.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.wwh.memo.R
import com.wwh.memo.logic.dao.MemoDAO
import com.wwh.memo.logic.model.Database
import com.wwh.memo.logic.model.Memo
import com.wwh.memo.logic.utils.Utils

class MemoContentActivity : AppCompatActivity() {
    private lateinit var memoDAO : MemoDAO

    companion object{  // 唤醒一个Activity的十分好用的方式
        fun actionStart(context: Context, memo : Memo) {
            val intent = Intent(context, MemoContentActivity::class.java).apply {
                putExtra("memo_title", memo.title)
                putExtra("memo_content", memo.content)
                putExtra("memo_id", memo.id)
                putExtra("memo_datetime", memo.datetime)
            }
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.memo_content)

        memoDAO = MemoDAO(this, Database.DATABASE_VERSION)

        setSupportActionBar(findViewById(R.id.toolbarContent))  // 设置标题栏
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
        }

        val title = intent.getStringExtra("memo_title")
        val content = intent.getStringExtra("memo_content")
        val datetime = intent.getStringExtra("memo_datetime")
        findViewById<EditText>(R.id.memoTitle).setText(title)
        findViewById<EditText>(R.id.memoContent).setText(content)
        findViewById<TextView>(R.id.memoDatetime).setText(datetime)

    }

    override fun onDestroy() {
        super.onDestroy()
        memoDAO.destroy()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar_content, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = intent.getIntExtra("memo_id", -1)
        val title = findViewById<EditText>(R.id.memoTitle).text.toString()
        val content = findViewById<EditText>(R.id.memoContent).text.toString()
        val datetime = Utils.getDateTime()

        val memo = Memo(id, title, content, datetime)

        when(item.itemId) {
            android.R.id.home -> {
                val builder: AlertDialog.Builder = AlertDialog.Builder(this)
                builder
                    .setTitle("退出编辑")
                    .setMessage("请确认是否已保存,点击“确认”退出编辑页面，点击“取消”继续编辑备忘录。")
                    .setPositiveButton("确认") { dialog, which ->
                        finish()
                    }
                    .setNegativeButton("取消") { dialog, which ->
                    }

                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
            R.id.backup -> Toast.makeText(this, "You clicked Backup", Toast.LENGTH_SHORT).show()
            R.id.delete -> {
                if(memo.id == -1) {
                    Toast.makeText(this, "备忘录未保存，可直接退出", Toast.LENGTH_SHORT).show()
                } else {
                    val builder: AlertDialog.Builder = AlertDialog.Builder(this)
                    builder
                        .setTitle("确认删除")
                        .setPositiveButton("确认") { dialog, which ->
                            memoDAO.deleteMemo(memo)
                            Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show()
                            finish()
                        }
                        .setNegativeButton("取消") { dialog, which ->
                        }

                    val dialog: AlertDialog = builder.create()
                    dialog.show()

                }

            }
            R.id.settings -> Toast.makeText(this, "You clicked Settings", Toast.LENGTH_SHORT).show()
            R.id.save -> {
                if(memo.id == -1) {
                    memoDAO.addMemo(memo)
                } else {
                    memoDAO.updateMemo(memo)
                }
                findViewById<TextView>(R.id.memoDatetime).text = datetime
                Toast.makeText(this, "保存成功！", Toast.LENGTH_SHORT).show()
            }
        }

        return true
    }
}